package com.wisdudu.blank.ui.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wisdudu.blank.R;
import com.wisdudu.blank.support.rxbus.RxBusFlag;
import com.wisdudu.blank.support.view.ExceptionViewPager;
import com.wisdudu.blank.support.view.IndicatorView;
import com.wisdudu.blank.ui.common.adapter.ImageGlancePagerAdapter;
import com.wisdudu.frame.base.BaseActivity;

import java.util.ArrayList;


/**
 * Created by qyf on 2016/5/5.
 * 图片缩放实现，注意Intent传入参数
 * 1.RxBusFlag.IMAGE_GLANCE_PATHS,传入图片路径List<String>
 * 2.RxBusFlag.CURRENT_PHOTO_INDEX，当前选中index，从0开始
 * 3.RxBusFlag.IS_SHOW_SAVE,是否显示titlebar，右上角有保存按钮，默认不显示
 */
public class ImageGlanceActivity extends BaseActivity {
    ExceptionViewPager mViewpager;
    IndicatorView mIndicatorView;
    private ArrayList<String> paths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_image_glance);
        mViewpager = (ExceptionViewPager) findViewById(R.id.viewpager);
        mIndicatorView = (IndicatorView) findViewById(R.id.indicator_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (getIntent().getBooleanExtra(RxBusFlag.IS_SHOW_SAVE, false)) {
            toolbar.setVisibility(View.VISIBLE);
            TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
            toolbarTitle.setText("图片预览");
//            toolbar.setTitle("图片预览");
            toolbar.setNavigationIcon(R.drawable.ic_toolbar_back);
            toolbar.inflateMenu(R.menu.menu_fragment_confirm);
            toolbar.setNavigationOnClickListener(v -> finish());
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if (item.getItemId() == R.id.menu_confirm) {
                        Logger.d("点击确定");
                        Intent intent = new Intent();
                        ArrayList<String> selectedPaths = new ArrayList<>();
                        selectedPaths.add(paths.get(mViewpager.getCurrentItem()));
                        intent.putStringArrayListExtra(RxBusFlag.SELECT_IMGS, selectedPaths);
                        ImageGlanceActivity.this.setResult(RESULT_OK, intent);
                        finish();
                        return true;
                    }
                    return false;
                }
            });
        } else {
            toolbar.setVisibility(View.GONE);
        }
//        mTvTitle.setText("图片预览");
        paths = getIntent().getStringArrayListExtra(RxBusFlag.IMAGE_GLANCE_PATHS);
        mIndicatorView.setEnabled(true);
        mIndicatorView.init(paths.size());
        mIndicatorView.selectTo(getIntent().getIntExtra(RxBusFlag.CURRENT_PHOTO_INDEX, 0));
        ImageGlancePagerAdapter imageGlancePagerAdapter = new ImageGlancePagerAdapter(getSupportFragmentManager(), paths);
        mViewpager.setAdapter(imageGlancePagerAdapter);
        mViewpager.setCurrentItem(getIntent().getIntExtra(RxBusFlag.CURRENT_PHOTO_INDEX, 0));
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndicatorView.selectTo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public ArrayList<String> getPaths() {
        return paths;
    }

//    @OnClick(R.id.iv_back)
//    public void onClick() {
//        finish();
//    }

}
